package com.cxm.qyyz.core.http.interceptor;

import android.os.Build;
import android.text.TextUtils;

import com.cxm.qyyz.BuildConfig;
import com.cxm.qyyz.SPManager;
import com.cxm.qyyz.UserManager;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class TokenInterceptor implements Interceptor {

    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request original = chain.request();
        UserManager instance = UserManager.getInstance();
        String token = instance.getToken();
        if (TextUtils.isEmpty(token)) {
            token = "";
        }
//        boolean time = SPManager.getTime();
//        if (time){
//            String deviceId = Tracking.getDeviceId();
//            if (deviceId.length() < 5){
//                App.getInstance().initTK(false);
//            }
//        }

        String mobileType = Build.MODEL;
        Request request = original.newBuilder()
                .addHeader("Authorization", token)
                .addHeader("xVersion", BuildConfig.VERSION_NAME)
                .addHeader("xMobiletype", TextUtils.isEmpty(mobileType) ? "" : mobileType)
                .build();
        return chain.proceed(request);
    }
}
